package com.studor.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.annotation.SaCheckRole;
import com.studor.common.Result;
import com.studor.dto.RuleCreateRequest;
import com.studor.entity.Rule;
import com.studor.service.RuleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import jakarta.validation.Valid;

import java.util.List;

@RestController
@RequestMapping("/api/rule")
public class RuleController {
    
    @Autowired
    private RuleService ruleService;
    
    @GetMapping("/list")
    @SaCheckLogin
    public Result<List<Rule>> list() {
        List<Rule> rules = ruleService.list();
        return Result.success(rules);
    }
    
    @GetMapping("/available")
    @SaCheckLogin
    public Result<List<Rule>> getAvailableRules(@RequestParam String major,
                                                @RequestParam String gender) {
        List<Rule> rules = ruleService.getAvailableRules(major, gender);
        return Result.success(rules);
    }
    
    @PostMapping("/add")
    @SaCheckLogin
    @SaCheckRole("admin")
    public Result<Void> add(@Valid @RequestBody RuleCreateRequest request) {
        Rule rule = new Rule();
        rule.setRuleName(request.getRuleName());
        rule.setRuleType(request.getRuleType());
        rule.setRuleContent(request.getRuleContent());
        rule.setScope(request.getScope());
        rule.setPriority(request.getPriority());
        rule.setDescription(request.getDescription());
        
        ruleService.save(rule);
        return Result.success("添加选宿规则成功");
    }
    
    @PutMapping("/update")
    @SaCheckLogin
    @SaCheckRole("admin")
    public Result<Void> update(@RequestBody Rule rule) {
        ruleService.updateById(rule);
        return Result.success("更新选宿规则成功");
    }
    
    @DeleteMapping("/delete/{id}")
    @SaCheckLogin
    @SaCheckRole("admin")
    public Result<Void> delete(@PathVariable Integer id) {
        ruleService.removeById(id);
        return Result.success("删除选宿规则成功");
    }
} 